﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using TS3QueryLib.Core.Query;

namespace TS3ServerManager.Controls
{
    public class ControlBase : UserControl
    {
        #region Non Public Members

        private static bool? _isInDesignMode;

        #endregion


        #region Properties

        protected QueryRunner QueryRunner { get; private set; }

        public App Application { get { return (App)System.Windows.Application.Current; } }

        #endregion

        #region Non Public Methods

        protected void RunIfNotDisposed(Action logic)
        {
            if (QueryRunner.IsDisposed)
                return;

            logic();
        }

        protected void RunOnUIThread(Action logic)
        {
            if (Dispatcher.CheckAccess())
                logic();
            else
                Dispatcher.Invoke(logic);
        }

        public static bool IsInDesignMode
        {
            get
            {
                if (!_isInDesignMode.HasValue)
                {
#if SILVERLIGHT
                    _isInDesignMode = DesignerProperties.IsInDesignTool;
#else
                    _isInDesignMode = (bool)DependencyPropertyDescriptor.FromProperty( DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
#endif
                }

                return _isInDesignMode.Value;
            }
        }

        #endregion
    }
}
